/*
 * Copyright 2011-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sabayframework.queue;

import org.sabayframework.activity.Activity;
import org.sabayframework.activity.ActivityMonitor;
import org.sabayframework.channel.Channel;


/**
 * Execution queue interface.
 * 
 * @author Alex Antonau
 * */
public interface ExecutionQueue {

	/**
	 * Submits an activity to the execution queue.
	 * @param activity activity to submit to the execution queue
	 * @param input input channel for the activity
	 * @param output output channel for the activity
	 * @throws IllegalStateException if this queue is not active
	 * */
	<I,O> ActivityMonitor<I,O> submit(Activity<I,O> activity, Channel<I> input, Channel<O> output);
	
	/**
	 * Tries to execute given activity in current thread (while activity is ready) and then if needed 
	 * submits it to the execution queue.
	 * @param activity activity to be executed in current thread 
	 * @param input input channel for the activity
	 * @param output output channel for the activity
	 * @throws IllegalStateException if this queue is not active
	 * */
	<I,O> ActivityMonitor<I,O> execute(Activity<I,O> activity, Channel<I> input, Channel<O> output);
	
	/**
	 * Returns number of worker threads servicing this queue. 
	 */
	int workerCount();
	/**
	 * Returns number of activities currently in the queue
	 * */
	int size();
	/**
	 * Returns true if there is no activities in the queue, false - otherwise
	 * */
	boolean isEmpty();
	
	/**
	 * Tries to awake an activity that reads from the given input channel. 
	 * */
	void channelChanged(Channel<?> input);
	
	/**
	 * Stops this execution queue.
	 * */
	void stop();
}
